<?php

namespace app\admin\business;

class AdminUser
{

    public function __construct()
    {
        $this->adminUserObj = new \app\common\model\mysql\AdminUser();
    }

    public function login($data){
        try {
            $adminUser = $this->getAdminUserByUsername($data['username']);
            if($adminUser['password'] != md5($data['password']."_singwa_abc")){
//                return show(config('status.error'),'登陆失败,密码错误');
                throw new \think\Exception("登陆失败,密码错误");
            }

            //记录用户信息
            $updateData = [
                'last_login_time' => time(),
                'last_login_ip' => request()->ip(),
            ];
            $adminUserObj = $this->adminUserObj;
            $res = $adminUserObj->updateById($adminUser['id'], $updateData);
            if (empty($res)){
                throw new \think\Exception("登录失败,更新数据失败");
//                return show(config('status.error'),'登录失败,更新数据失败');
            }
            session(config("admin.session_admin"),$adminUser);
        }catch (\Exception $e){
//            return show(config('status.error'),'内部异常,登录失败');
            throw new \think\Exception($e->getMessage());
        }
        return true;
    }


    public function getAdminUserByUsername($username){
        $adminUserObj = $this->adminUserObj;
        $adminUser = $adminUserObj->getAdminUserByUsername($username);
        if(empty($adminUser)){
            return [];
        }
        return $adminUser->toArray();
    }
}